from ..model import db, session, User, Transaction, and_, datetime, func


def add_user_do(name, rmk):
    if name == '':
        return {
            'error': 'true',
            'message': '姓名不能为空'
        }
    else:
        if session.query(User).filter(User.name == name).first() is not None:
            return {
                'error': 'true',
                'message': '用户已存在'
            }
        else:
            session.add(User(name=name, rmk=rmk))
            db.commit(session)
            return {
                'error': 'false',
                'message': '操作成功'
            }


def get_user_list_do(name, trasc_status, page_num):
    query = session.query(User.name, User.ttlInc, User.ttlSpd)
    results = None
    offset = (page_num - 1) * 10
    if name == '' and trasc_status == '':
        results = query.order_by(User.crtTm.desc()).offset(offset).limit(10).all()
    elif name != '' and trasc_status == '':
        results = query.filter(User.name == name).order_by(
            User.crtTm.desc()).offset(offset).limit(10).all()
    elif name == '' and trasc_status != '':
        results = query.filter(
            User.trascStatus == trasc_status
        ).order_by(User.crtTm.desc()).offset(offset).limit(10).all()
    elif name != '' and trasc_status != '':
        results = query.filter(and_(
            User.name == name,
            User.trascStatus == trasc_status
        )).order_by(User.crtTm.desc()).offset(offset).limit(10).all()
    data_count = len(session.query(User).all())
    data = [
        {
            'name': result[0],
            'ttlInc': result[1],
            'ttlSpd': result[2],

        }
        for result in results
    ]
    return {
        'dataCount': data_count,
        'dataList': data
    }


def get_user_dtl_do(name):
    result = session.query(
        User.name,
        User.rmk,
        User.ttlInc,
        User.ttlSpd,
        User.trascStatus,
        User.crtTm
    ).filter(
        User.name == name
    ).first()
    return {
        'name': result[0],
        'rmk': result[1],
        'ttlInc': result[2],
        'ttlSpd': result[3],
        'trascStatus': result[4],
        'crtTm': result[5].strftime('%Y-%m-%d %H:%M:%S')
    }


def update_user_do(name, rmk):
    session.query(User).filter(User.name == name).update(
        {
            'rmk': rmk,
            'updTm': datetime.now()
        }
    )
    db.commit(session)
    return {
        'error': 'false',
        'message': '操作成功'
    }


def delete_user_do(name):
    user_id = session.query(User).filter(User.name == name).first().usrId
    if session.query(Transaction).filter(Transaction.trascUsrId == user_id).first() is None:
        session.query(User).filter(User.usrId == user_id).delete()
        db.commit(session)
        return {
            'error': 'false',
            'message': '操作成功'
        }
    else:
        return {
            'error': 'true',
            'message': '该用户存在交易信息'
        }
